package com.znf4.controller.notice;

import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.async.DeferredResult;

import com.znf4.controller.websocket.SystemWebSocketHandler;
import com.znf4.notice.entity.Notice;
import com.znf4.notice.enums.NoticeStatusEnum;
import com.znf4.notice.server.NoticeServer;

/**
 * 活动和公告
 */
@Controller
@RequestMapping("/merchant/notice")
public class NoticeController {
	
	private static final Logger LOG = LoggerFactory.getLogger(NoticeController.class);
	
	@Autowired
	private NoticeServer noticeServer ;
	
	@Bean
	public SystemWebSocketHandler systemWebSocketHandler() {
		return new SystemWebSocketHandler();
	}
	
	private static DeferredResult<String> deferredResult  ;  // 公告信息
	

	
	/**
	 * 函数功能说明 ： 获取一条公告推送
	 * 方式：短链接轮询
	 * @return
	 */
	@RequestMapping(value = "/queryNotice" , produces = "text/event-stream;charset=UTF-8")
	@ResponseBody
	public DeferredResult<String> queryNotice(){
	
		System.out.println("开始");
		deferredResult = new DeferredResult<String>() ;
		System.out.println("输出");
		
		return  deferredResult ;
	} 
	
	/**
	 * 定时推送
	 */
	@Scheduled(fixedDelay = 2000 )
	public void reFresh(){
		
		LOG.info("开始循环");		
		if(deferredResult != null){
			
	        LOG.info("不为null");		
	        Notice notice = new Notice() ;
	        notice.setStatus(NoticeStatusEnum.ACTIVE.getDesc());
			/** 去数据库中查*/
	        noticeServer.queryOne(notice) ;
	        Random r = new Random() ;
			deferredResult.setResult("这是一条公告 :" + r.nextInt()) ;
		}
		
		
	}
	
	
	
	
	
	
	

}
